import UIKit
//多次执行 for和while循环
//基于特定个条件执行 if和switch，还有控制流程跳转的break和continue语句
//For循环 1.for-in 2.for添加递增 已经不支持了
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
//如果你不想知道区间内的每项值，可用 _ 代替变量名
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
}

let names = ["Anna", "Alex", "Brain", "Jack"]
for name in names {
    print("hello, \(name)")
}

let numberOfLegs = ["spider" : 8, "ant" : 6, "cat" : 4]
for (name, number) in numberOfLegs {
    print("\(name) have \(number) legs.")
}
//字符串也可以遍历
for c in "Hello" {
    print(c)
}

//for var index = 0; index< 3; ++index {
//    print("index is \(index)")
//}

//while 循环。 运行一系列语句直到条件为false

var index = 0, sum = 0
while (index < 10) {
    sum += index
    index += 1
}

var i = 0
repeat {
    i += 1
} while (i < 10)

//if
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
    
} else {
    
}

//switch。把某个值与若个模式进行匹配。根据第一个匹配成功的模式，switch会执行对应的代码。switch语句必须是完备的，每个可能的值必须至少有一个case分支与之对应。在某些不可能覆盖所有条件的情况下，使用默认default分支满足该要求，这个默认分支必须在switch语句的最后面。
//switch some value to consider {
//case value1:
//    respond to value1
//case value2, value3:
//    respond to 2 or 3
//default:
//    otherwise,do something else
//}
//下面例子使用switch匹配一个名为 someCharacter的小写字母,第一个case匹配元音，第二个case匹配bcdfghj中的辅音字母
let someCharacter = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
    print("\(someCharacter) is 元音字母")
case "b", "c", "d","f", "g","h", "j":
    print("\(someCharacter) is in bcdfghj")
default:
    print("default")
}

//Swift中的switch语句不需要写break.
//每个case分支必须包含至少一条语句。下面写法错误。
let anotherCharacter = "a"
switch anotherCharacter {
//case "a": //这个分支是空的，不允许，Swift不会同时匹配"a"和"A“
case "A":
    print("\(someCharacter) is letter A")
default:
    print("not the letter A")
}
//一个case也可以包含多个模式，用逗号把他们分开：
//switch some value to consider {
//case value1,value2:
//    statement
//}


//区间匹配 case分支的模式也可以是一个值的区间。
let count = 3_000_000_000_000
let countedThings = "stars in the Milty Way"
var naturalCount: String
switch count {
case 0:
    naturalCount = "no"
case 1...3:
    naturalCount = "a few"
case 4...9:
    naturalCount = "several"
case 10...99:
    naturalCount = "tens of"
case 100...999:
    naturalCount = "hundreds of"
case 1000...9999:
    naturalCount = "thousands of"
default:
    naturalCount = "millions and millons of"
}

print("There are \(naturalCount) \(countedThings)")

//元组
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
    print("(0, 0) is at the origin")
case (_, 0):
    print("(\(somePoint), 0) is on the x-aixs")
case (0, _) :
    print("(\(somePoint), 0) is on the y-aixs")
case (-2...2, -2...2):
    print("\(somePoint) is inside the box")
default:
    print("\(somePoint) is outside the box")
}
//swift允许多个case匹配同一个值，在这个例子中,(0,0)可以匹配所有四个case.但是如果存在多个匹配，那么只会执行第一个被匹配到的case.
//case分支的模式允许将匹配到的值绑定到一个临时的常量或变量中。这些常量或变量就可以在该case中被引用了。---这种行为被称为值绑定。
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
    print("on the x-axis with an x value of \(x)")
case (0, let y):
    print("on the y-axis with an y value of \(y)")
case (let x, let y):
    print("somewhere else at \(x), \(y)")
}
//这个switch没有default分支，因为最后一个case已经可以匹配余下所有条件了。

//where case分支模式可以用where语句来判断额外的条件
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    print("is on the line x == y")
case let (x, y) where x == -y :
    print("is on the line x == -y")
case let (x, y):
    print("is just some arbitray point")
}
//这三个case都声明了常量x和y的占位符，用于临时获取元祖yetAnotherPoint的两个值。这些常量被用做where的一部分，从而创建一个动态的过滤器filter.当且仅当where语句的条件为true时，匹配到的case分支才会被执行。

//转移控制语句
//continue  告诉一个循环体立即停止本次循环，重新开始下次循环迭代。
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput {
    switch character {
    case "a", "e", "i", "o", "u":
        continue
    default:
        puzzleOutput.append(character)
    }
}
puzzleOutput

//break 立即结束整个控制流的执行。然后跳转到表示循环体结束的大括号后的第一行代码。
//switch语句中的break:会中断switch代码块的执行，并且跳转到表示switch代码块结束的大括号后的第一行代码。

//fallthrough 如果你需要C风格的贯穿特性，你可以在每个需要该特型的case分支中使用fallthrough关键字。

let integerToDescibe = 3
var description = "The number \(integerToDescibe) is "
switch integerToDescibe {
case  2,3,5,7,11,13,17,19:
    description += "a prime number, and also"
    fallthrough //只会匹配下个case中执行的代码，没有case有default就执行default代码，什么都没有就会报错
case 22:
    description += " and is 22"
case 23:
    description += " and is 23"
    
default:
    description += " an integer"
}

